<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="炼厂名称">
          <a-input placeholder="请输入炼厂名称" v-decorator="['refineryName', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所在城市">
          <a-input placeholder="请输入所在城市" v-decorator="['city', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="详细地址">
          <a-input placeholder="请输入详细地址" v-decorator="['address', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="炼厂简介">
          <j-editor style="width: 100%" v-decorator="[ 'content', {} ]" triggerChange></j-editor>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="装车事项">
          <j-editor style="width: 100%" v-decorator="[ 'descript', {} ]" triggerChange></j-editor>
        </a-form-item>
        <a-form-item v-if="refineryId && type == 2" :labelCol="labelCol" :wrapperCol="wrapperCol" label="油原料">
          <a-select @change="handleChange" v-model="value2" mode="multiple" placeholder="请选择油原料">
            <a-select-option :value='i.oilRawMaterialId' v-for="(i,index) in list" :key="index">
              {{i.rawMaterialName}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-select @change="handleChange" v-model="status" placeholder="请选择状态">
            <a-select-option value='1'>
              显示
            </a-select-option>
            <a-select-option value='0'>
              隐藏
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否有优惠价">
          <a-select @change="handleChange" v-model="discountFlag" placeholder="请选择是否有优惠价">
            <a-select-option value='1'>
              有
            </a-select-option>
            <a-select-option value='0'>
              无
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    httpAction,
    postAction,
    getAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    components: {
      JEditor
    },
    props: {
      type: {
        type: Number,
        default: 1
      }
    },
    name: "YsyRefineryModal",
    data() {
      return {
        value2: [],
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          list: "/app/ysyOilRawMaterial/list",
          add: "/app/ysyRefinery/add",
          edit: "/app/ysyRefinery/edit",
          oilRawAdd: '/app/ysyRowMaterialRefinery/batchAdd',
          oilList: "/app/ysyRowMaterialRefinery/list",
        },
        refineryId: '',
        list: [],
        oilRawMaterialIds: [],
        indexs: [],
        status: '',
        discountFlag: '1'
      }
    },
    created() {},
    methods: {
      handleChange(e) {
        this.indexs = e
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if (record.status) {
          this.status = record.status.toString()
        }
        if (record.discountFlag) {
          this.discountFlag = record.discountFlag.toString()
        }
        if (record.refineryId) {
          this.refineryId = record.refineryId
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'refineryId', 'refineryName', 'city', 'address', 'content',
            'descript',
            'status', 'lat', 'lng'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.refineryId = ''
        this.value2 = []
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.refineryId) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.status = this.status
            formData.discountFlag = this.discountFlag
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                if (!that.model.refineryId) {
                  that.refineryId = res.message
                } else {
                  that.refineryId = that.model.refineryId
                }
                that.$message.success(res.message);
                this.$emit('ok');
                this.visible = false;
                // that.addOriRaw();
              } else {
                that.$message.warning(res.message);
                this.$emit('ok');
                this.visible = false;
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel() {
        this.close()
      },

    }
  }
</script>

<style lang="less" scoped>

</style>